﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using TracNghiemTinHocDaiCuong.ThucThe;
using TracNghiemTinHocDaiCuong.Common;

namespace TracNghiemTinHocDaiCuong.DAO
{
    public class BaiLamDAO
    {
        public static BaiLam DoiTuong(IDataRecord read)
        {
            return new BaiLam
            {
                MaBL = Methods.ConvertNumber(read["MaBL"].ToString()),
                MaND = Methods.ConvertNumber(read["MaND"].ToString()),
                MaDT = Methods.ConvertNumber(read["MaDT"].ToString()),
                MaCH = Methods.ConvertNumber(read["MaCH"].ToString()),
                DapAnND = Methods.ConvertString(read["DapAnND"].ToString())
            };
        }

        public static IList<BaiLam> LayDanhSach()
        {
            IList<BaiLam> danhSach = new List<BaiLam>();
            string query = string.Format("SELECT * FROM BaiLam");
            IDataReader read = SqlHelper.ExecuteReader(query);
            while(read.Read())
            {
                danhSach.Add(DoiTuong(read));
            }
            return danhSach;
        }
        public static bool Them(BaiLam baiLam)
        {
            string query = string.Format("INSERT INTO BaiLam(MaND, MaDT, MaCH, DapAnND) " +
            "VALUES(@MaND, @MaDT, @MaCH, @DapAnND)");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaND", baiLam.MaND),
                new SqlParameter("@MaDT", baiLam.MaDT),
                new SqlParameter("@MaCH", baiLam.MaCH),
                new SqlParameter("@DapAnND", baiLam.DapAnND)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }

        public static bool Sua(BaiLam baiLam)
        {
            string query = string.Format("UPDATE BaiLam SET MaND=@MaND, MaDT=@MaDT, MaCH=@MaCH, " +
            "DapAnND=@DapAnND WHERE MaBL=@MaBL");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaND", baiLam.MaND),
                new SqlParameter("@MaDT", baiLam.MaDT),
                new SqlParameter("@MaCH", baiLam.MaCH),
                new SqlParameter("@DapAnND", baiLam.DapAnND),
                new SqlParameter("@MaBL", baiLam.MaBL)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }

        public static bool Xoa(int idBaiLam)
        {
            string query = string.Format("DELETE FROM BaiLam WHERE MaBL=@MaBL");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaBL", idBaiLam)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }
    }
}